{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Make mel norm test data\n",
    "\n",
    "This notebook reads in one of the golden data files for the mel matrix function test in `test_filters.py:test_melfb`, then writes several variants of it including an additional `norm` field (to be provided as a `norm=` argument to `filters.py:mel`) and writes out three new golden data files.  These are consumed by the new `__test_with_norm` private function within `test_filters.py:test_melfb`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import numpy as np\n",
    "import librosa\n",
    "import scipy.io"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "LIBROSA_TEST_PATH = '.'\n",
    "infile = os.path.join(LIBROSA_TEST_PATH, 'data/feature-melfb-001.mat')\n",
    "DATA = scipy.io.loadmat(infile, chars_as_strings=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'wts': array([[ 0.        ,  0.00492891,  0.00985782, ...,  0.        ,\n",
      "         0.        ,  0.        ],\n",
      "       [ 0.        ,  0.        ,  0.        , ...,  0.        ,\n",
      "         0.        ,  0.        ],\n",
      "       [ 0.        ,  0.        ,  0.        , ...,  0.        ,\n",
      "         0.        ,  0.        ],\n",
      "       ..., \n",
      "       [ 0.        ,  0.        ,  0.        , ...,  0.        ,\n",
      "         0.        ,  0.        ],\n",
      "       [ 0.        ,  0.        ,  0.        , ...,  0.        ,\n",
      "         0.        ,  0.        ],\n",
      "       [ 0.        ,  0.        ,  0.        , ...,  0.        ,\n",
      "         0.        ,  0.        ]]), 'frqs': array([[    0.        ,    79.62501637,   159.25003274,   238.87504912,\n",
      "          318.50006549,   398.12508186,   477.75009823,   557.37511461,\n",
      "          637.00013098,   716.62514735,   796.25016372,   875.8751801 ,\n",
      "          955.50019647,  1036.88788871,  1125.62589636,  1221.95820045,\n",
      "         1326.53472923,  1440.06103253,  1563.30304193,  1697.09223823,\n",
      "         1842.33126133,  2000.        ]]), 'fmin': array([[0]], dtype=uint8), 'fmax': array([[2000]], dtype=uint16), 'sr': array([[8000]], dtype=uint16), 'nfilts': array([[20]], dtype=uint8), 'htk': array([[0]], dtype=uint8), '__header__': 'MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Thu Jan 23 18:28:46 2014', '__globals__': [], 'width': array([[1]], dtype=uint8), 'nfft': array([[256]], dtype=uint16), '__version__': '1.0'}\n"
     ]
    }
   ],
   "source": [
    "print DATA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 1)\n"
     ]
    }
   ],
   "source": [
    "DATA['norm'] = np.array([[1]])\n",
    "print DATA['norm'].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def melfbnorm_test(DATA):\n",
    "    # if DATA['norm'] is empty, pass None.\n",
    "    if DATA['norm'].shape[-1] == 0:\n",
    "        norm = None\n",
    "    else:\n",
    "        norm = DATA['norm'][0][0]\n",
    "\n",
    "    wts = librosa.filters.mel(DATA['sr'][0, 0],\n",
    "                               DATA['nfft'][0, 0],\n",
    "                               n_mels=DATA['nfilts'][0, 0],\n",
    "                               fmin=DATA['fmin'][0, 0],\n",
    "                               fmax=DATA['fmax'][0, 0],\n",
    "                               htk=DATA['htk'][0, 0], \n",
    "                               # if DATA['norm'] is empty, pass None.\n",
    "                               norm=norm)\n",
    "    padded_wts = np.pad(wts, [(0, 0), (0, int(DATA['nfft'][0]//2) - 1)], mode='constant')\n",
    "    return padded_wts\n",
    "\n",
    "padded_wts = melfbnorm_test(DATA)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(20, 256)\n",
      "(20, 256)\n"
     ]
    }
   ],
   "source": [
    "print padded_wts.shape\n",
    "print DATA['wts'].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "assert np.allclose(padded_wts, DATA['wts'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wrote ./data/feature-melfbnorm-001.mat\n"
     ]
    }
   ],
   "source": [
    "TEST_NUM = 1\n",
    "\n",
    "def save_next_test(data):\n",
    "    global TEST_NUM\n",
    "    outfile = os.path.join(LIBROSA_TEST_PATH, 'data/feature-melfbnorm-{:03d}.mat'.format(TEST_NUM))\n",
    "    scipy.io.savemat(outfile, data)\n",
    "    print 'wrote', outfile\n",
    "    TEST_NUM += 1\n",
    "    \n",
    "save_next_test(DATA)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 0)\n",
      "wrote ./data/feature-melfbnorm-002.mat\n"
     ]
    }
   ],
   "source": [
    "DATA['norm'] = np.array([[]])\n",
    "print DATA['norm'].shape\n",
    "padded_wts = melfbnorm_test(DATA)\n",
    "# It's different from the last golden value.\n",
    "assert not np.allclose(padded_wts, DATA['wts'])\n",
    "# Make it the new golden value for this test.\n",
    "DATA['wts'] = padded_wts\n",
    "\n",
    "save_next_test(DATA)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wrote ./data/feature-melfbnorm-003.mat\n"
     ]
    }
   ],
   "source": [
    "DATA['norm'] = np.array([[np.inf]])\n",
    "padded_wts = melfbnorm_test(DATA)\n",
    "# It's the same as the last golden value.\n",
    "assert np.allclose(padded_wts, DATA['wts'])\n",
    "\n",
    "save_next_test(DATA)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
